#! /usr/bin/env python
# -*- coding:utf-8 -*-
# ====#====#====#====

# 导包
import unittest
import requests
from ddt import ddt, data, unpack
from common.readExcle import ReadExcle
from common.preSolve import PreSolve
from common.configHttp import ConfigHttp
from common.writeExcle import WriteExcle
from common.log import logger

# 实例化获取数据
read_ex = ReadExcle()
read_open = read_ex.read()

# 定义testCase类
@ddt
class testCase(unittest.TestCase):
    @data(*read_open)
    @unpack
    def testcase(self, id, interfaceUrl, name, method, value, header, rely, caseid, expect, real, status):
        # 转换数据格式
        value = eval(value)
        header = eval(header)
        expect = int(expect)

        # 调用执行前置用力方法执行前置用例
        prev = PreSolve(read_open)
        header, value = prev.presolve(id)
        # 调用前置用例执行方法
        re_prev = ConfigHttp(method, interfaceUrl, eval(value), eval(header)).get_method()

        # 设置返回值格式
        re_dict = re_prev.json()
        real = re_dict['errorCode']
        try:
            if real == expect:
                status = 'pass'
            else:
                status = 'fail'
            wr = WriteExcle().write_exlce(int(id), 9, real, status)
        except Exception as msg:
            logger.info('接口调用失败：%s'%msg)
            raise
        self.assertEqual(real, expect, '接口测试不通过')

if __name__ == '__main__':
    unittest.main(verbosity=2)